package com.lst.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lst.api.constant.DeleteEnum;
import com.lst.api.entity.LoanPurpose;
import com.lst.api.mapper.LoanPurposeMapper;
import com.lst.api.service.ILoanPurposeService;
import com.lst.api.vo.IsInputCodeAndNameVo;
import com.lst.tools.mybatis.service.impl.BaseService;
import com.lst.tools.mybatis.utils.ExpandSqlUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 贷款用途管理表业务层实现
 *
 * @author Frank
 * @date 2019-12-30 10:31:37
 * @version: V1.0
 */
@Service
public class LoanPurposeService extends BaseService<LoanPurposeMapper, LoanPurpose> implements ILoanPurposeService {

    @Override
    public List<IsInputCodeAndNameVo> listLoanPurpose() {
        QueryWrapper<LoanPurpose> queryWrapper = new QueryWrapper();
        queryWrapper.eq(LoanPurpose.IS_DELETE, DeleteEnum.DELETE_NO.getKey());
        queryWrapper
                .orderByAsc(ExpandSqlUtils.getIsNullOrderBy(LoanPurpose.SORT, false))
                .orderByAsc(LoanPurpose.SORT)
                .orderByDesc(LoanPurpose.CREATE_AT)
                .orderByDesc(LoanPurpose.ID);
        List<LoanPurpose> loanPurposes = super.list(queryWrapper);
        if (CollectionUtils.isEmpty(loanPurposes)) {
            return Collections.EMPTY_LIST;
        }
        return loanPurposes
                .stream()
                .map(item -> new IsInputCodeAndNameVo(item.getId().toString(), item.getTitle(), item.getIsInput()))
                .collect(Collectors.toList());
    }
}